package com.linkoog.devtools.intellij.ui.table.row_data;


import com.intellij.icons.AllIcons;
import com.intellij.util.ui.ColumnInfo;
import com.intellij.util.ui.ListTableModel;
import com.linkoog.devtools.intellij.ui.table.ListTableView;
import com.linkoog.devtools.intellij.ui.table.TableRowData;
import com.linkoog.devtools.intellij.ui.table.cell_editor.StringDocumentHandler;
import com.linkoog.devtools.intellij.ui.table.column_info.column.ActionColumnInfo;
import com.linkoog.devtools.intellij.ui.table.column_info.column.CheckBoxColumnInfo;
import com.linkoog.devtools.intellij.ui.table.column_info.column.StringColumnInfo;
import lombok.Data;
import lombok.EqualsAndHashCode;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

@Data
@EqualsAndHashCode(callSuper = false)
public class KeyValWithCheckedTableRowData extends TableRowData {
    private boolean checked;
    private String key;
    private String value;

    public static ListTableView<KeyValWithCheckedTableRowData> createListTableView() {
        ListTableView.TablePopupMenuProvider<KeyValWithCheckedTableRowData> popupMenuProvider = (table, rowData, cell, row, column) -> {
            return null;
        };

        ListTableView.EmptyRowProvider<KeyValWithCheckedTableRowData> emptyRowProvider = () -> {
            KeyValWithCheckedTableRowData keyValWithCheckedTableRowData = new KeyValWithCheckedTableRowData();
            keyValWithCheckedTableRowData.setChecked(false);
            keyValWithCheckedTableRowData.setKey("");
            keyValWithCheckedTableRowData.setValue("");
            return keyValWithCheckedTableRowData;
        };

        StringDocumentHandler documentHandler = (documentEvent, table, oldCell, newCell, row, column) -> {
            if (table.getEditingRow() == table.getRowCount() - 1) {

                ListTableView<KeyValWithCheckedTableRowData> listTableView = (ListTableView<KeyValWithCheckedTableRowData>) table;
                ListTableModel<KeyValWithCheckedTableRowData> model = listTableView.getListTableModel();

                KeyValWithCheckedTableRowData rowData = model.getItem(row);
                rowData.setChecked(true);

                ColumnInfo<KeyValWithCheckedTableRowData,Object> columnInfo = model.getColumnInfos()[column];
                columnInfo.setValue(rowData, newCell);
                //添加空白的最后一行
                listTableView.createEmptyRow().ifPresent(model::addRow);
                table.editCellAt(row, column);
            }
        };



        ListTableView<KeyValWithCheckedTableRowData> listTableView = new ListTableView<>(popupMenuProvider, emptyRowProvider);
        CheckBoxColumnInfo<KeyValWithCheckedTableRowData> columnInfo1 =  new CheckBoxColumnInfo<>(" ", "checked", table -> 20);
        StringColumnInfo<KeyValWithCheckedTableRowData> columnInfo2 = new StringColumnInfo<>("Name", "key", null, documentHandler);
        StringColumnInfo<KeyValWithCheckedTableRowData> columnInfo3 = new StringColumnInfo<>("Value", "value", null, documentHandler);
        ActionColumnInfo<KeyValWithCheckedTableRowData> columnInfo4 = new ActionColumnInfo<>(" ", AllIcons.Actions.Cancel, (table, columnInfo, rowData, cellValue, row, column) -> {
            if (row != table.getRowCount()-1){
                table.getListTableModel().removeRow(row);
                table.getListTableModel().fireTableDataChanged();
            }
        });



        ListTableModel<KeyValWithCheckedTableRowData> listTableModel = new ListTableModel<>(columnInfo1, columnInfo2, columnInfo3, columnInfo4);
        listTableView.setModelAndUpdateColumns(listTableModel);

        return listTableView;

    }

    public static Map<String, List<String>> getKeyValue(ListTableView<KeyValWithCheckedTableRowData> listTableView) {
        List<KeyValWithCheckedTableRowData> items = listTableView.getListTableModel().getItems();
        Map<String, List<String>> result = new LinkedHashMap<>();
        items.stream().filter(KeyValWithCheckedTableRowData::isChecked)
                .forEach(item -> {
                    List<String> list = result.getOrDefault(item.getKey(), new ArrayList<>());
                    list.add(item.getValue());
                    result.put(item.getKey(), list);
                });
        return result;
    }

    public static void reloadData(ListTableView<KeyValWithCheckedTableRowData> listTableView, Map<String, String> params) {
        if (params != null && !params.isEmpty()){
            List<KeyValWithCheckedTableRowData> list = new ArrayList<>();
            params.forEach((key, value) -> {
                KeyValWithCheckedTableRowData keyValWithCheckedTableRowData = new KeyValWithCheckedTableRowData();
                keyValWithCheckedTableRowData.setChecked(true);
                keyValWithCheckedTableRowData.setKey(key);
                keyValWithCheckedTableRowData.setValue(value);
                list.add(keyValWithCheckedTableRowData);
            });
            listTableView.reloadData(list);
        }
    }




}
